package com.woniu.woniu_bx.controller;

import com.woniu.woniu_bx.groups.AddGroup;
import com.woniu.woniu_bx.pojo.Discuss;
import com.woniu.woniu_bx.pojo.Moment;
import com.woniu.woniu_bx.pojo.ResponseEntity;
import com.woniu.woniu_bx.service.DiscussService;
import com.woniu.woniu_bx.service.MomentService;
import com.woniu.woniu_bx.pojo.User;
import com.woniu.woniu_bx.vo.MomentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @ClassName CommunityController
 * @Description 社区交流
 * @Author 魏才林
 * @Date 2022/9/1 9:55
 * @Version 1.0
 **/
@Slf4j
@Validated
@RestController
@RequestMapping("/community")
public class CommunityController {

    @Autowired
    private MomentService momentService;
    @Autowired
    private DiscussService discussService;

    /**
     * @return com.woniu.woniu_bx.pojo.ResponseEntity
     * @Description 动态新增
     * @Date 10:39 2022/9/1
     * @Author 魏才林
     * @Param [moment]
     **/
    @PostMapping("/add")
    public ResponseEntity add(@RequestBody @Validated(AddGroup.class) Moment moment) {
        momentService.add(moment);
        return ResponseEntity.SUCCESS("ok", null);
    }

    /**
     * @return com.woniu.woniu_bx.pojo.ResponseEntity
     * @Description 获得所有动态
     * @Date 11:01 2022/9/1
     * @Author 魏才林
     * @Param [moment]
     **/
    @GetMapping("/getAllMoment")
    public ResponseEntity getAllMoment(@Min(1) @NotNull Integer pageNum, Integer userId) {
        log.debug("pageNum: {}, userId: {}", pageNum, userId);
        Integer pageSize = 5;
        return ResponseEntity.SUCCESS("ok", momentService.getAllMoment(pageNum, pageSize, userId));
    }

    /**
     * 根据用户id查询所有的动态
     *
     * @param userId
     * @return
     */
    @GetMapping("/getUserMoments/{myId}/{userId}")
    public ResponseEntity getUserMoments(@PathVariable Integer userId, @PathVariable Integer myId) {
        MomentVo momentVo = momentService.getByUserId(userId, myId);

        return ResponseEntity.SUCCESS("ok", momentVo);
    }

    //查询我关注的人的信息（id，头像，昵称，动态，动态评论）
    @PostMapping("/mylike/{id}")
    public ResponseEntity<List<User>> findByMyLikeId(@PathVariable(name = "id") int id) {
        List<User> users = momentService.findByMyLikeId(id);
        if (users != null) {
            return ResponseEntity.SUCCESS("查询成功", users);
        } else {
            return ResponseEntity.FAIL("无查询结果", null);
        }
    }

    //查看详情
    @RequestMapping("/details/{id}")
    public ResponseEntity<Moment> details(@PathVariable(name = "id") int id) {
        Moment details = momentService.details(id);
        return ResponseEntity.SUCCESS("查看详情成功", details);
    }

    // 点赞
    @RequestMapping("/giveLike/{userId}/{momentId}")
    public ResponseEntity<Moment> giveLike(@PathVariable(name = "userId") int userId,
                                           @PathVariable(name = "momentId") int momentId) {
        momentService.giveLike(userId, momentId);
        return ResponseEntity.SUCCESS("点赞成功", null);
    }

    // 消赞
    @RequestMapping("/cancelLike/{userId}/{momentId}")
    public ResponseEntity<Moment> cancelLike(@PathVariable(name = "userId") int userId,
                                             @PathVariable(name = "momentId") int momentId) {
        momentService.cancelLike(userId, momentId);
        return ResponseEntity.SUCCESS("取消点赞成功", null);
    }

    /**
     * 发布评论
     *
     * @param discuss
     * @return
     */
    @RequestMapping("/discuss/add")
    public ResponseEntity discussAdd(@RequestBody Discuss discuss) {
        discussService.add(discuss);
        System.out.println(discuss);
        return ResponseEntity.SUCCESS("评论成功", null);
    }

    /**
     * 删除评论
     *
     * @param id
     * @return
     */
    @RequestMapping("/discuss/deldiscuss/{id}")
    public ResponseEntity discussDelete(@PathVariable(name = "id") int id) {
        discussService.removeById(id);
        return ResponseEntity.SUCCESS("删除成功", null);
    }

    //查询动态所有评论
    @RequestMapping("/alldisscuss/{id}")
    public ResponseEntity<List<Discuss>> findAllDisscussByMomentId(@PathVariable(name = "id") int id) {
        List<Discuss> discusses = discussService.findAllDisscussByMomentId(id);
        return ResponseEntity.SUCCESS("评论组件需求信息", discusses);
    }

    //重新添加评论方法
    @RequestMapping("/discuss/adddiscuss")
    public ResponseEntity<Discuss> addDiscuss(@RequestBody Discuss discuss) {
        Discuss discuss1 = discussService.addDiscuss(discuss);
        return ResponseEntity.SUCCESS("评论成功", discuss1);
    }

    // 删除动态
    @RequestMapping("/del/{id}")
    public ResponseEntity<String> deleteMoment(@NotNull @PathVariable Integer id){
        momentService.deleteById(id);
        return ResponseEntity.SUCCESS("删除成功",null);
    }
}
